package com.dz.controller.visitor;

import com.dz.annotation.OperationLog;
import com.dz.bean.Booking;
import com.dz.service.BookingService;
import com.dz.util.Page;
import com.dz.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/v/booking")
public class BookingController {

    @Autowired
    private BookingService bookingService;

    // 添加预约
    @PostMapping
    @OperationLog("添加预约")
    public Result add(@RequestBody Booking booking) {
        try {
            Booking result = bookingService.add(booking);
            if (result != null) {
                // 预约成功后，查询该用户所有预约信息
                Page page = new Page(); // 可根据需求设置分页参数
                page.setPageSize(10);
                page.setCurrentPage(1);
                bookingService.findByVisitorId(page, booking.getVisitorId());
                return Result.success("预约成功", page);
            } else {
                return Result.error(500, "预约失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "预约失败: " + e.getMessage());
        }
    }

    // 取消预约
    @DeleteMapping("/{id}")
    @OperationLog("取消预约")
    public Result delete(@PathVariable("id") Integer id) {
        try {
            int result = bookingService.delete(id);
            if (result > 0) {
                return Result.success("取消预约成功");
            } else {
                return Result.error(500, "取消预约失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "取消预约失败: " + e.getMessage());
        }
    }

    // 修改预约
    @PutMapping
    @OperationLog("修改预约信息")
    public Result edit(@RequestBody Booking booking) {
        try {
            Booking result = bookingService.edit(booking);
            if (result != null) {
                return Result.success("修改预约成功", result);
            } else {
                return Result.error(500, "修改预约失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "修改预约失败: " + e.getMessage());
        }
    }

    // 查询所有预约
    @GetMapping("/all")
    public Result all(Booking booking) {
        try {
            return Result.success(bookingService.all(booking));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "查询失败: " + e.getMessage());
        }
    }

    // 根据ID查询预约详情
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Booking result = bookingService.findById(id);
            if (result != null) {
                return Result.success(result);
            } else {
                return Result.error(404, "预约信息不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "查询失败: " + e.getMessage());
        }
    }

    // 根据游客ID分页查询预约信息
    @GetMapping("/visitor/{visitorId}")
    public Result findByVisitorId(Page page, @PathVariable("visitorId") Integer visitorId) {
        try {
            bookingService.findByVisitorId(page, visitorId);
            return Result.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "查询失败: " + e.getMessage());
        }
    }
}
